package com.sixdoors.business.service.impl;

import com.sixdoors.business.domain.ArticleContent;
import com.sixdoors.business.domain.ArticlePart;
import com.sixdoors.business.mapper.ArticleContentMapper;
import com.sixdoors.business.mapper.ArticlePartMapper;
import com.sixdoors.business.service.IArticlePartService;
import com.sixdoors.common.exception.ServiceException;
import com.sixdoors.common.utils.DateUtils;
import com.sixdoors.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 文章部位Service业务层处理
 * 
 * @author sixdoors
 * @date 2024-04-08
 */
@Service
public class ArticlePartServiceImpl implements IArticlePartService 
{
    @Resource
    private ArticlePartMapper articlePartMapper;

    @Resource
    private ArticleContentMapper articleContentMapper;

    /**
     * 查询文章
     * 
     * @param id 文章部位主键
     * @return 文章
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ArticlePart selectArticlePartById(Long id)
    {

        ArticlePart articlePart = articlePartMapper.selectArticlePartById(id);

        // 浏览量 +1
        long viewVolume = articlePart.getViewVolume() + 1L;
        articlePart.setViewVolume(viewVolume);
        articlePartMapper.updateArticlePart(articlePart);

        ArticleContent content = articleContentMapper.selectArticleContentByPartId(articlePart.getId());
        articlePart.setContent(content.getArticleContent());

        return articlePart;
    }

    /**
     * 查询文章部位列表
     * 
     * @param articlePart 文章部位
     * @return 文章部位
     */
    @Override
    public List<ArticlePart> selectArticlePartList(ArticlePart articlePart)
    {
        return articlePartMapper.selectArticlePartList(articlePart);
    }

    /**
     * 新增文章部位
     * 并新增文章内容
     * @param articlePart 文章部位
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertArticlePart(ArticlePart articlePart)
    {
        Long userId = SecurityUtils.getUserId();
        Date nowDate = DateUtils.getNowDate();
        articlePart.setCreateTime(nowDate);
        articlePart.setCreateBy(userId.toString());
        articlePart.setUpdateBy(userId.toString());
        articlePart.setUpdateTime(nowDate);

        if (articlePartMapper.insertArticlePart(articlePart) <= 0) {
            return 0;
        }

        // 新增文章内容
        ArticleContent content = new ArticleContent();
        content.setArticlePartId(articlePart.getId());
        if (articleContentMapper.insertArticleContent(content) <= 0){
            throw new ServiceException("新增失败！",500);
        }

        return 1;
    }

    /**
     * 修改文章部位
     * 
     * @param articlePart 文章部位
     * @return 结果
     */
    @Override
    public int updateArticlePart(ArticlePart articlePart)
    {

        Long userId = SecurityUtils.getUserId();
        articlePart.setUpdateBy(userId.toString());
        articlePart.setUpdateTime(DateUtils.getNowDate());
        if(articlePartMapper.updateArticlePart(articlePart) <= 0){
            return  0;
        }

        ArticleContent content = articleContentMapper.selectArticleContentByPartId(articlePart.getId());
        if (Objects.isNull(content)){
            content = new ArticleContent();
        }
        content.setArticlePartId(articlePart.getId());
        content.setArticleContent(articlePart.getContent());
        if (articleContentMapper.updateArticleContent(content) <= 0){
            throw new ServiceException("修改失败！",500);
        }

        return 1;
    }

    /**
     * 批量删除文章部位
     * 
     * @param ids 需要删除的文章部位主键
     * @return 结果
     */
    @Override
    public int deleteArticlePartByIds(Long[] ids)
    {
        return articlePartMapper.deleteArticlePartByIds(ids);
    }

    /**
     * 删除文章部位信息
     * 
     * @param id 文章部位主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteArticlePartById(Long id)
    {
        if (articlePartMapper.deleteArticlePartById(id) <= 0){
            throw new ServiceException("删除失败：删除文章部位失败",500);
        }

        if (articleContentMapper.deleteArticleContentByPartId(id) <= 0){
            throw new ServiceException("删除失败：删除文章内容失败",500);
        }
        return 1;
    }
}
